home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / SAT 2.3.8 / Demos / StepPlatform Demo ƒ / StepPlatform.p < prev    next >
Text File  |  1996-05-11  |  9KB  |  271 lines

  1. { StepPlatform by Nissan Zafrir, based on MyPlatform and StepZkrolly}
  2.  
  3. { Modifications by Ingemar R:}
  4. { Scroll-length timed after TickCount in order to make it fast enough on slow Macs}
  5. { Replaced ox/oy by gSAT.wind.port->portRect.left and .top (ox/oy are obsolete)}
  6. { B/W icons and patterns}
  7. { Centered window}
  8. { Aligned scrolling to multiples of 8 (to account for a limitation in the 1-bit and 4-bit blitters)}
  9.  
  10.  
  11. (*}
  12. {Desirable improvements:}
  13. {• The man should look better}
  14. {• Some kind of opponents?}
  15. {• Ladders?}
  16. {• Better, more general rect-bounce utilities? (I.e. using SATToolbox.)}
  17. {*)
  18.  
  19.  
  20. program StepPlatform;
  21.  
  22.     uses
  23. {$ifc UNDEFINED THINK_PASCAL}
  24.         Types, QuickDraw, Menus, Windows, TextEdit, Fonts, Dialogs,{}
  25.         Memory, ToolUtils, Events, OSUtils,{}
  26. {$endc}
  27.         SAT, MySlotVBL, PlatformGlobals, sPlayerSprite, sPlatForm, sHMovPlatform, sMovPlatform, sEmptyPlatform, InformUser;
  28.  
  29.     var
  30.         thepat: SATPatHandle;
  31.         ignoreSp: SpritePtr;
  32.         p: Point;
  33. {playerSp: SpritePtr;}
  34.         gWind: WindowPtr;
  35.         gVBLInstalled: Boolean;
  36.         r: Rect;
  37.  
  38.     const
  39.         scrollSizeH = 512;
  40.         scrollSizeV = 384;
  41.  
  42. {playerSp^.position -> viewPoint}
  43. {scrollSizeH -> ?}
  44. {}
  45. {(Ingemar's private notes)}
  46. {Mer att fixa: Kan ScrollScreen göras så den alltid scrollar direkt, i ett steg?}
  47. {Då skulle "vanlig" scroll fixa sig med samma rutin! Specialfall av scrollSpeed?}
  48. {Nytt namn - SATScroll? Eller om två rutiner, SATScroll och SATStepScroll?}
  49.  
  50.     function ScrollScreen (viewPoint: Point; marginH, marginV, scrollSpeed: Integer): Boolean;
  51. {const}
  52. {sco = 8;        {the speed of the scroll, number of pixels per *tick*}
  53.         var
  54.             startTicks, frameTime, step: LongInt;
  55.             srcRect: Rect;
  56.             where, were: Point;
  57.             nowOff: Point;
  58.             scrollSizeH, scrollSizeV: Integer;
  59.         function max (x, y: Integer): Integer;
  60.         begin
  61.             if x > y then
  62.                 max := x
  63.             else
  64.                 max := y;
  65.         end; {max}
  66.         function min (x, y: Integer): Integer;
  67.         begin
  68.             if x < y then
  69.                 min := x
  70.             else
  71.                 min := y;
  72.         end; {min}
  73.     begin
  74.         scrollSizeH := gSAT.wind.port^.portRect.right - gSAT.wind.port^.portRect.left;
  75.         scrollSizeV := gSAT.wind.port^.portRect.bottom - gSAT.wind.port^.portRect.top;
  76.  
  77.         frameTime := 1;
  78. { If the player sprite is at the border, scroll!}
  79.         if ((viewPoint.h + marginH > scrollSizeH + gSAT.wind.port^.portRect.left) or (viewPoint.h - marginH < gSAT.wind.port^.portRect.left) or (viewPoint.v + marginV > scrollSizeV + gSAT.wind.port^.portRect.top) or (viewPoint.v - marginV < gSAT.wind.port^.portRect.top)) then
  80. {gSAT.wind.port->portRect.left/top = ox/oy!}
  81.             begin
  82.                 SATSetPortScreen;
  83.                 nowOff := gSAT.wind.port^.portRect.topLeft; {Get old origin}
  84.                 were := nowOff;
  85.                 where := viewPoint;
  86.                 where.h := where.h - BSR(scrollSizeH, 1);
  87.                 where.v := where.v - BSR(scrollSizeV, 1);
  88.                 if where.h < 0 then
  89.                     where.h := 0;
  90.                 if where.v < 0 then
  91.                     where.v := 0;
  92.                 if where.h + scrollSizeH > gSAT.offSizeH then
  93.                     where.h := gSAT.offSizeH - scrollSizeH;
  94.                 if where.v + scrollSizeV > gSAT.offSizeV then
  95.                     where.v := gSAT.offSizeV - scrollSizeV;
  96.  
  97.                 where.h := BitAnd(where.h, $fff8); { Scroll only to multiples of 8, so we won't confuse the 4-bit and 1-bit blitters!}
  98.  
  99.                 repeat
  100.                     begin
  101.                         startTicks := TickCount;
  102.  
  103.                         step := scrollSpeed * frameTime;
  104.                         if (nowOff.h > where.h) then
  105.                             nowOff.h := max(nowOff.h - step, where.h);
  106.                         if (nowOff.h < where.h) then
  107.                             nowOff.h := min(nowOff.h + step, where.h);
  108.                         if (nowOff.v > where.v) then
  109.                             nowOff.v := max(nowOff.v - step, where.v);
  110.                         if (nowOff.v < where.v) then
  111.                             nowOff.v := min(nowOff.v + step, where.v);
  112.                         SetOrigin(nowOff.h, nowOff.v);
  113.                         gSAT.wind.bounds := gSAT.wind.port^.portRect; { Synch gSAT.wind.bounds with the portRect!}
  114.                         srcRect := gSAT.wind.port^.portRect;
  115.  
  116.                         CopyBits(gSAT.offScreen.port^.portBits, gSAT.wind.port^.portBits, srcRect, srcRect, srcCopy, nil);
  117.  
  118.                         frameTime := TickCount - startTicks;
  119.                     end;
  120.                 until not ((nowOff.h <> where.h) or (nowOff.v <> where.v));
  121.             end;
  122.     end; {ScrollScreen}
  123.  
  124.     procedure SetupWind;
  125.  
  126.         var
  127.             zr: Rect;
  128.             wrld: SysEnvRec;
  129.  
  130.     begin
  131.     {• Since SAT hasn't been initialized, we can't use gSAT.colorFlag but }
  132.     {• have to check environs ourselves.}
  133.         if (noErr <> SysEnvirons(1, wrld)) then
  134.             ;{• ignore errors.}
  135.  
  136. {    SetRect(&zr, (512-scrollSizeH)/2, 0,  (512-scrollSizeH)/2 + scrollSizeH, 0 + scrollSizeV);}
  137. { no- center on screen instead!}
  138.         SetRect(zr, 0, 0, scrollSizeH, scrollSizeV);
  139. {$IFC UNDEFINED THINK_PASCAL}
  140.         OffsetRect(zr, (qd.screenBits.bounds.right - qd.screenBits.bounds.left - scrollSizeH) div 2, (qd.screenBits.bounds.bottom - qd.screenBits.bounds.top - scrollSizeV) div 2);
  141. {$ELSEC}
  142.         OffsetRect(zr, (screenBits.bounds.right - screenBits.bounds.left - scrollSizeH) div 2, (screenBits.bounds.bottom - screenBits.bounds.top - scrollSizeV) div 2);
  143. {$ENDC}
  144.         if (wrld.hasColorQD) then
  145.             gWind := NewCWindow(nil, zr, '', false, plainDBox, WindowPtr(-1), false, 0)
  146.         else
  147.             gWind := NewWindow(nil, zr, '', false, plainDBox, WindowPtr(-1), false, 0);
  148.     end; {SetupWind}
  149.  
  150.     function IsOptionPressed: Boolean;
  151.         var
  152.             km: KeyMap;
  153.     begin
  154.         GetKeys(km);
  155.         IsOptionPressed := km[56];
  156.     end;
  157.  
  158. {main }
  159.  
  160.     var
  161.         tempRect: Rect;
  162.         e: EventRecord;
  163.         startTicks: Longint;
  164.         where: Point;
  165.  
  166. begin
  167. {$IFC UNDEFINED THINK_PASCAL}
  168.     SATInitToolbox;
  169.     GetDateTime(qd.randSeed);
  170. {$ELSEC}
  171.     GetDateTime(randSeed);
  172. {$ENDC}
  173.  
  174.     SATConfigure(true, kVPositionSort, kBackwardCollision, 64);
  175.     SetupWind;
  176.  
  177.     SetRect(r, 0, 0, 1000, 484);    {the offscreen size}
  178.     SATCustomInit(0, 0, r, gWind, nil, false, false, false, true, false);
  179.     SATSetSpriteRecSize(sizeof(Sprite));
  180.  
  181.     ShowWindow(gSAT.wind.port);
  182.     SelectWindow(gSAT.wind.port);
  183.     SATHideMBar(gWind);
  184.  
  185.     (* fill the backscreen in a pattren *)
  186.     SATSetPortBackScreen;
  187.     if (IsOptionPressed) then
  188.         thepat := SATGetPat(SATRand(5) + 128)    { choose a Pat in Random}
  189.     else
  190.         thepat := SATGetPat(128);{128-brown wall, 130-gray wall pattren}
  191.     SATPenPat(thepat);
  192.     SetRect(tempRect, 0, 0, gSAT.offSizeH, gSAT.offSizeV + 100);
  193.     PaintRect(tempRect);
  194.     CopyBits(gSAT.backScreen.port^.portBits, gSAT.offScreen.port^.portBits, gSAT.offScreen.port^.portRect, gSAT.offScreen.port^.portRect, srcCopy, nil);
  195.     SATBackChanged(gSAT.offScreen.port^.portRect);
  196.     SATRedraw;
  197.  
  198.     (*Initialize all sprite units*)
  199.     InitPlayerSprite;
  200.     InitPlatform;
  201.     InitEmptyPlatform;
  202.     InitMovPlatform;
  203.     InitHMovPlatform;
  204.     InitInformationArea;
  205.  
  206.     SATRedraw;
  207.  
  208.     (* SetUp my Sprites    *)
  209.     GetMouse(p);
  210.     playerSp := PlSpritePtr(SATNewSprite(1, p.h, p.v, @SetupPlayerSprite));    { Keep Player Sprite}
  211.     ignoreSp := SATNewSprite(0, 0, 350, @SetupEmptyPlatform);    { the Floor Platform}
  212.     SetRect(ignoreSp^.hotRect, 0, 0, gSAT.offSizeH - 150, 20);
  213.     ignoreSp := SATNewSprite(0, 55, 300, @SetupPlatform);        { Standing Platform}
  214.     ignoreSp := SATNewSprite(0, 245, 235, @SetupPlatform);        { Standing Platform}
  215.     ignoreSp := SATNewSprite(55, 355, 200, @SetupMovPlatform);    {50=MinV    200=MaxV}
  216.         {ignoreSp->position.h=30;    // I can change the defeults}
  217.     ignoreSp := SATNewSprite(100, 55, 200, @SetupMovPlatform);    {100=MinV    230=MaxV}
  218.     ignoreSp := SATNewSprite(128, 270, 120, @SetupHMovPlatform);    {120=MinH   150=MaxH}
  219.     ignoreSp^.speed.h := 2;
  220.  
  221.     ignoreSp := SATNewSprite(250, 850, 400, @SetupMovPlatform);    {50=MinV    200=MaxV}
  222.  
  223.     ignoreSp := SATNewSprite(55, 700, 250, @SetupMovPlatform);        {50=MinV    200=MaxV}
  224.     ignoreSp := SATNewSprite(430, 630, 85, @SetupHMovPlatform);    {50=MinV    200=MaxV}
  225.  
  226.     (*Update the game window once more, so the pattern and what we drawn in DrawInfo are shown. *)
  227.     SATRedraw;
  228.  
  229.     HideCursor;
  230.     SATSoundOff;
  231.  
  232. {if (gSAT.colorFlag) then { Can't do SlotVInstall without CQD, but MySLotVBL handles that!}
  233.         if (IsOptionPressed) then
  234.         if InstallVBL(gSAT.wind.device) = noErr then
  235.             begin
  236.                 SATInstallSynch(@SATSynch);    {WaitForSync}
  237.                 SysBeep(7);
  238.                 gVBLInstalled := true;
  239.             end;
  240.  
  241. (* the main loop *)
  242.  
  243.     gSAT.wind.bounds := gSAT.wind.port^.portRect;
  244.     SATSetPortScreen;
  245.     SATRedraw;
  246.     repeat
  247.         startTicks := TickCount; {If not VBL-synched}
  248.         SATRun(true);    {!IsOptionPressed()}
  249.         where.h := playerSp^.position.h + 32;
  250.         where.v := playerSp^.position.v + 32;
  251.         if ScrollScreen(where, 32, 32, 8) then
  252.             ;
  253.         if IsOptionPressed then
  254.             DrawProgrammerInfo;
  255. {if (IsOptionPressed()) if (WaitNextEvent ( everyEvent, &e, 10, nil )) ;}
  256.  
  257.         if not gVBLInstalled then while TickCount < startTicks + 1 do
  258.             ; {Max 60 fps}
  259.     until Button;
  260.  
  261. (* cleanning up *)
  262.     SATSetPortScreen;
  263. {    if gVBLInstalled then}
  264. {    RemoveVBLCounter;}
  265.     SATSoundShutup;            {Free sound channels}
  266.     SATShowMBar(gWind);        {Restore the menu bar}
  267.     RemoveVBL;                {Turn off the VBL task, if any}
  268.  
  269.     ShowCursor;
  270.     FlushEvents(everyEvent, 0);
  271. end. {main}